package com.kust.smarthome.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.kust.smarthome.web.dto.EvaluateDto;
import com.kust.smarthome.web.mapper.EvaluateMapper;
import com.kust.smarthome.web.mapper.OrderFormMapper;
import com.kust.smarthome.web.model.Evaluate;
import com.kust.smarthome.web.model.OrderForm;
import com.kust.smarthome.web.service.IEvaluateService;
import com.kust.smarthome.web.vo.EvaluateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-12
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements IEvaluateService {

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private OrderFormMapper orderFormMapper;

    @Override
    @Transactional
    public int insertEvaluate(Integer userId, EvaluateVo evaluateVo) {

        Evaluate e = new Evaluate()
                .setUserId(userId)
                .setCommodityId(evaluateVo.getCommodityId())
                .setOrderFormId(evaluateVo.getOrderFormId())
                .setInfo(evaluateVo.getInfo())
                .setImgurl1(evaluateVo.getImgurl1())
                .setImgurl2(evaluateVo.getImgurl2())
                .setImgurl3(evaluateVo.getImgurl3())
                .setImgurl4(evaluateVo.getImgurl4())
                .setCreated(LocalDateTime.now());

        OrderForm orderForm = orderFormMapper.selectById(evaluateVo.getOrderFormId());
        orderForm.setState(4);
        orderFormMapper.updateById(orderForm);

        return evaluateMapper.insert(e);
    }

    @Override
    public List<EvaluateDto> getEvaluateByCommodityId(Integer commodityId) {

        List<EvaluateDto> list = evaluateMapper.getEvaluateByCommodityId(commodityId);

        return list;
    }
}
